/**
 * @Author chentl
 * @Version V1.0
 * @Project wh-bigdata-frame.
 * @Title SessionRepository
 * @Description 用户Session存储中心
 * 正式场景下，应该使用redis或者数据库来存储session信息；
 * @Package com.echo.sdk.im.server.respo
 * @Date 2021/6/18 3:47 下午
 * @Copyright: 武汉大数据产业发展有限公司 All rights reserved.
 */
package com.echo.sdk.im.respo;

import cn.hutool.core.collection.CollectionUtil;
import com.echo.sdk.im.model.CIMSession;
import org.springframework.stereotype.Repository;

import java.util.concurrent.ConcurrentHashMap;

@Repository
public class SessionRepository {

    private final ConcurrentHashMap<String, ConcurrentHashMap<String, CIMSession>> session_map = new ConcurrentHashMap<>();

    public void save1(String userId, String nioId, CIMSession session) {
        ConcurrentHashMap<String, CIMSession> currentMap = session_map.get(userId);

        if (CollectionUtil.isEmpty(currentMap)) {
            ConcurrentHashMap<String, CIMSession> userMap = new ConcurrentHashMap<>();
            userMap.put(nioId, session);
            session_map.put(userId, userMap);
        } else {
            currentMap.put(nioId, session);
            session_map.put(userId, currentMap);
        }

    }

    public CIMSession get1(String userId, String nioId) {
        return session_map.get(userId).get(nioId);
    }

    public void remove1(String nioId) {
        for (ConcurrentHashMap<String, CIMSession> chl : session_map.values()) {
            chl.remove(nioId);
        }
    }

    /*
     * @Author chentl
     * @Description 获取当前的session_map
     * @Date 3:51 下午 2021/6/18
     * @Param []
     * @Return java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.ConcurrentHashMap<java.lang.String,com.echo.sdk.im.server.model.CIMSession>>
     **/
    public ConcurrentHashMap<String, ConcurrentHashMap<String, CIMSession>> getSessionMap() {
        return session_map;
    }


}
